package phantom

import (
	"fmt"

	"gddgame.cc/galaxy/utils/def"
)

var (
	platformMap = make(map[string]PlatformHandle)
)

type PlatformHandle func(config *Config) (Platform, error)

type Platform interface {
	Recorder() def.Recorder
}

func RegisterPlatform(name string, platform PlatformHandle) error {
	if _, ok := platformMap[name]; ok {
		return fmt.Errorf("Platform is exist: %s", name)
	}
	platformMap[name] = platform
	return nil
}

func PlatformWithConfig(config *Config) (Platform, error) {
	platform, ok := platformMap[config.Platform]
	if ok {
		return platform(config)
	} else {
		return nil, fmt.Errorf("Platform not exist: %s", config.Platform)
	}
}
